package com.smarkparking.controller;

import com.smarkparking.pojo.Order;
import com.smarkparking.pojo.Parking;
import com.smarkparking.pojo.User;
import com.smarkparking.pojo.Userproposal;
import com.smarkparking.service.ParkingService;
import com.smarkparking.service.UserService;
import com.smarkparking.service.UserproposalService;
import com.smarkparking.service.impl.OrderServiceImpl;
import com.smarkparking.tool.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 刘涛
 * @create 2021/11/17 14:44
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ParkingService parkingService;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private UserproposalService userproposalService;
    @Autowired
    private VerificationCode verificationCode;

    @RequestMapping("/add")
    public int addUser(@RequestParam("pass") String password, @RequestParam("email") String email, @RequestParam("number") String licensePlateNumber, @RequestParam("name") String name) {
        User user = new User();
        user.setEmail(email);
        user.setName(name);
        user.setPassword(password);
        user.setLicensePlateNumber(licensePlateNumber);
        int i = 0;
        try {
            i = userService.addUser(user);
        } finally {
            return i;
        }
    }

    @RequestMapping("/findByEmail")
    public boolean findByEmail(@RequestParam("email") String email, @RequestParam("pass") String pass, HttpSession session) {
        try {
            User user = userService.selectUserByEmail(email);
            String password = user.getPassword();
            if (password.equals(pass) && email != null) {
                session.setAttribute("userSession", email);
                System.out.println("session:"+session.getAttribute("userSession"));
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @RequestMapping("/findParkingAddress")
    public List<String> findParkingAddress() {
        return parkingService.findParkingAddress();
    }

    @RequestMapping("/getUserSession")
    public Object getUserSession(HttpSession session) {
        System.out.println("session:"+session.getAttribute("userSession"));
        return session.getAttribute("userSession");
    }

    @RequestMapping("/findParkingByAddress")
    public List<Parking> findParkingByAddress(@RequestParam("address") String address) {
        List<Parking> parkings = parkingService.findParkingByAddress(address);
        return parkings;
    }

    @RequestMapping("/addOrder")
    public boolean addOrder(@RequestParam("id") Integer id, @RequestParam("email") String email) {
        boolean b = orderService.addOrder(id, email);
        return b;
    }

    @RequestMapping("/findCompleteOrder")
    public List<Order> findCompleteOrder(@RequestParam("email") String email) {
        List<Order> completeOrder = orderService.findCompleteOrder(email);
        return completeOrder;
    }

    @RequestMapping("/findNoCompleteOrder")
    public List<Order> findNoCompleteOrder(@RequestParam("email") String email) {
        List<Order> completeOrder = orderService.findNoCompleteOrder(email);
        return completeOrder;
    }

    @RequestMapping("/outParking")
    public void outParking(@RequestParam("id") Long id) {
        orderService.outParking(id);
    }

    @RequestMapping("/pay")
    public boolean pay(@RequestParam("id") Long id) {
        return orderService.pay(id);
    }

    @RequestMapping("/findName")
    public String findName(@RequestParam("email") String email) {
        User user = userService.selectUserByEmail(email);
        return user.getName();
    }

    @RequestMapping("/findProposalByEmail")
    public List<Userproposal> findProposalByEmail(@RequestParam("email") String email) {
        return userproposalService.findProposalByEmail(email);
    }

    @RequestMapping("/addProposal")
    public void addProposal(@RequestParam("email") String email, @RequestParam("message") String proposal) {
        userproposalService.addProposal(email, proposal);
    }

    @RequestMapping("/code")
    public Integer code(@RequestParam("email") String email) {
        Integer code = verificationCode.code(email);
        return code;
    }
}
